package searcher.gui;

/**
 * A TreeCellRenderer for rendering files--renders directories, files, and
 * zipped files.
 * 
 * @version $Id: FileRenderer.java,v 1.2 2006/04/05 17:47:25 abutler Exp $
 */
public class FileRenderer extends javax.swing.tree.DefaultTreeCellRenderer
{
	private static final javax.swing.Icon DIRECTORY_ICON;

	private static final javax.swing.Icon ZIP_ICON;

	private static final javax.swing.Icon FILE_ICON;

	static
	{
		java.net.URL url;
		url = FileRenderer.class.getResource("images/DirectoryIcon.png");
		if(url == null)
			DIRECTORY_ICON = null;
		else
			DIRECTORY_ICON = new javax.swing.ImageIcon(url);
		url = FileRenderer.class.getResource("images/ZipIcon.png");
		if(url == null)
			ZIP_ICON = null;
		else
			ZIP_ICON = new javax.swing.ImageIcon(url);
		url = FileRenderer.class.getResource("images/FileIcon.png");
		if(url == null)
			FILE_ICON = null;
		else
			FILE_ICON = new javax.swing.ImageIcon(url);
	}

	/**
	 * @param aFile The file to get the icon for
	 * @return An Icon to represent <code>aFile</code>
	 */
	public static javax.swing.Icon getIconFor(java.io.File aFile)
	{
		if(aFile instanceof de.schlichtherle.io.File
			&& ((de.schlichtherle.io.File) aFile).isArchive())
			return ZIP_ICON;
		else if(aFile.isDirectory())
			return DIRECTORY_ICON;
		else
			return FILE_ICON;
	}

	public java.awt.Component getTreeCellRendererComponent(
		javax.swing.JTree tree, Object value, boolean sel, boolean expanded,
		boolean leaf, int row, boolean focused)
	{
		super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf,
			row, focused);
		if(value instanceof java.io.File)
		{
			java.io.File f = (java.io.File) value;
			setText(f.getName());
			setIcon(getIconFor(f));
		}
		return this;
	}
}
